home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / wpj1_8.zip / SHAREMEM.ZIP / MALLOC / MALLOC.C next >
C/C++ Source or Header  |  1993-06-10  |  4KB  |  144 lines

  1. /*
  2.  
  3. MALLOC.C - Windows Global Memory Allocation Library
  4.        Initialisation/main module
  5.  
  6. */
  7.  
  8.  
  9. #pragma hdrfile "MALLOC.SYM"
  10. #define STRICT
  11. #include <windows.h>
  12. #include <winstyle.h>
  13. #include <dos.h>
  14. #pragma hdrstop
  15.  
  16. #include <wmalloc.h>
  17.  
  18.  
  19. #define TEST_ID        0x41A5
  20.  
  21.  
  22. static HWND hWnd = NULL;
  23. static HINSTANCE hInst;
  24. static char *TEST_CLASS = "TestClass",
  25.         *TESTMESSAGE = MALLOC_MESSAGE;
  26. static UINT uTestMsg;
  27.  
  28.  
  29. LRESULT CALLBACK TestProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  30. static void _gethwnd (void);
  31.  
  32.  
  33. // Procedure to initialise the DLL.
  34. #pragma argsused
  35. int CALLBACK LibMain (HINSTANCE hInstance, WORD DataSeg,
  36.               WORD HeapSize, LPSTR lpArgv)
  37.   {WNDCLASS cl;
  38.  
  39.    // Store the instance handle of the DLL for future use
  40.    hInst = hInstance;
  41.  
  42.    // Register communications class
  43.    cl.style = CS_GLOBALCLASS;
  44.    cl.lpfnWndProc = TestProc;
  45.    cl.cbClsExtra = 0;
  46.    cl.cbWndExtra = 0;
  47.    cl.hInstance = hInst;
  48.    cl.hIcon = NULL;
  49.    cl.hCursor = NULL;
  50.    cl.hbrBackground = NULL;
  51.    cl.lpszMenuName = NULL;
  52.    cl.lpszClassName = TEST_CLASS;
  53.    if (!RegisterClass (&cl)) return 0;
  54.  
  55.    uTestMsg = RegisterWindowMessage (TESTMESSAGE);
  56.  
  57.    // If the data segment is locked, unlock it
  58.    if (HeapSize) UnlockData (0);
  59.  
  60.    return 1;
  61.   } // end LibMain
  62.  
  63. // DLL termination procedure
  64. #pragma argsused
  65. int FAR PASCAL WEP (int iParam)
  66.   {return 1;
  67.   } // end WEP
  68.  
  69.  
  70. LRESULT CALLBACK TestProc (HWND hWndTest, UINT msg, WPARAM wParam, LPARAM lParam)
  71.   {if (msg == WM_COMMAND)
  72.      {if (wParam == TEST_ID && HIWORD (lParam) == MAL_ACK)
  73.     {hWnd = (HWND) LOWORD (lParam);
  74.     } // endif
  75.       return 0;
  76.      } // endif
  77.    return DefWindowProc (hWndTest, msg, wParam, lParam);
  78.   } // end TestProc
  79.  
  80.  
  81. static void _gethwnd (void)
  82.   {HWND hwndTest;
  83.  
  84.    hwndTest = CreateWindow (TEST_CLASS, "", WS_OVERLAPPED,
  85.       0, 0, 0, 0, NULL, NULL, hInst, NULL);
  86.    if (hwndTest == NULL) return;
  87.    ShowWindow (hwndTest, SW_HIDE);
  88.    SendMessage (HWND_BROADCAST, uTestMsg, (WPARAM) hwndTest,
  89.       MAKELPARAM (0, TEST_ID));
  90.    DestroyWindow (hwndTest);
  91.  
  92.    if (hWnd == NULL)
  93.      {WinExec ("_MALLOC.EXE", SW_HIDE);
  94.       hwndTest = CreateWindow (TEST_CLASS, "", WS_OVERLAPPED,
  95.      0, 0, 0, 0, NULL, NULL, hInst, NULL);
  96.       if (hwndTest == NULL) return;
  97.       ShowWindow (hwndTest, SW_HIDE);
  98.       SendMessage (HWND_BROADCAST, uTestMsg, (WPARAM) hwndTest,
  99.      MAKELPARAM (0, TEST_ID));
  100.       DestroyWindow (hwndTest);
  101.      } // endif
  102.   } // end gethwnd
  103.  
  104.  
  105. void far * WINAPI gmalloc (DWORD dwSize)
  106.   {void far *lpPtr;
  107.  
  108.    if (hWnd == NULL) _gethwnd();
  109.    if (hWnd == NULL) return NULL;
  110.    lpPtr = (void far *) SendMessage (hWnd, MAL_ALLOC, 0, dwSize);
  111.    return lpPtr;
  112.   } // end gmalloc
  113.  
  114.  
  115. void far * WINAPI grealloc (void far *lpPtr, DWORD dwSize)
  116.   {REALLOCSTRUCT Ra;
  117.    void far *lpPoint;
  118.  
  119.    if (hWnd == NULL) return NULL;
  120.    Ra.dwSize = dwSize;
  121.    Ra.lpPtr = lpPtr;
  122.    lpPoint = (void far *) SendMessage (hWnd, MAL_REALLOC, 0,
  123.       (LPARAM) (LPREALLOCSTRUCT) &Ra);
  124.    return lpPoint;
  125.   } // end grealloc
  126.  
  127.  
  128. void WINAPI gfree (void far *lpPtr)
  129.   {if (hWnd == NULL) return;
  130.    SendMessage (hWnd, MAL_FREE, 0, (LPARAM) lpPtr);
  131.   } // end gfree
  132.  
  133.  
  134. BOOL WINAPI gisptr (void far *lpPtr)
  135.   {HGLOBAL handle;
  136.  
  137.    handle = (HGLOBAL) LOWORD (GlobalHandle ((UINT) FP_SEG (lpPtr)));
  138.    if (handle == NULL) return FALSE;
  139.    if (FP_OFF (lpPtr) != NULL) return FALSE;
  140.    return TRUE;
  141.   } // end gisptr
  142.  
  143.  
  144.